﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;

namespace CommonHelper
{
    /// <summary>
    /// 反射工具
    /// </summary>
    public static class ReflexHelper
    {
        #region 对象相关

        /// <summary>
        /// 取对象属性值
        /// </summary>
        /// <param name="FieldName"></param>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static object GetFieldValue( object obj,string FieldName)
        {
            object res;
            Type Ts = obj.GetType();
            res = Ts.GetProperty(FieldName)?.GetValue(obj, null);
            return res;
        }

        /// <summary>
        /// Source克隆至Target
        /// </summary>
        /// <param name="Source"></param>
        /// <param name="Target"></param>
        /// <returns></returns>
        public static void Clone(object Source, object Target)
        {
            Type sourceType = Source.GetType();
            Type targetType = Target.GetType();
            var sourcePropertys = sourceType.GetProperties();
            var targetPropertys = targetType.GetProperties();
            foreach (var sourceProperty in sourcePropertys)
            {
                if (targetPropertys.Any(x => x.Name == sourceProperty.Name))
                {
                    targetType.GetProperty(sourceProperty.Name)
                        .SetValue(Target, sourceType.GetProperty(sourceProperty.Name).GetValue(Source), null);
                }
            }
        }


        /// <summary>
        /// 对比对象各属性是否都相等
        /// </summary>
        /// <param name="Source"></param>
        /// <param name="Target"></param>
        /// <returns></returns>
        public static bool Compare(object Source, object Target)
        {
            Type sourceType = Source.GetType();
            Type targetType = Target.GetType();

            var sourcePropertys = sourceType.GetProperties();
            var targetPropertys = targetType.GetProperties();

            foreach (var sourceProperty in sourcePropertys)
            {
                PropertyInfo info = targetPropertys.Where(x => x.Name == sourceProperty.Name).FirstOrDefault();

                var v1 = targetType.GetProperty(info.Name).GetValue(Target, null);
                var v2 = sourceType.GetProperty(sourceProperty.Name).GetValue(Source, null);


                var v1Tem = v1 == null ? "" : v1.ToString();
                var v2Tem = v2 == null ? "" : v2.ToString();
                if (v1Tem != v2Tem)
                {
                    return false;
                }
            }
            return true;
        }
        /// <summary>
        /// 设置对象属性值
        /// </summary>
        /// <param name="FieldName"></param>
        /// <param name="Value"></param>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static bool SetFieldValue(object obj,string FieldName, string Value)
        {
            try
            {
                Type Ts = obj.GetType();
                object v = Convert.ChangeType(Value, Ts.GetProperty(FieldName).PropertyType);
                Ts.GetProperty(FieldName).SetValue(obj, v, null);
                return true;
            }
            catch
            {
                return false;
            }
        }

        #endregion 对象相关
    }
}
